package com.vf.admin.miaodong.controller.fiwork;

import com.vf.admin.common.BaseController;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppTask;
import com.vf.admin.miaodong.service.AfterTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/manage/app/task")
public class AppTaskManageController extends BaseController {


    @Autowired
    private AfterTaskService taskService;

    @RequestMapping("/page")
    public String page(SplitPageBean splitPage, String taskname, String startDate, String endDate) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("taskname", taskname);
            params.put("startDate", startDate);
            params.put("endDate", endDate);
            List<AppTask> list = taskService.findList(params, splitPage);
            request.setAttribute("list", list);
            request.setAttribute("splitPage", splitPage);
            return this.sucess("/web/manage/app/task/list");
        } catch (Exception e) {
            e.printStackTrace();
            return this.error("系统繁忙");
        }
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Result saveOrUpate(AppTask params) {
        if (params.getId() == null) {
            params.setCreatetime(new Date());
        }
        taskService.saveOrUpdate(params);
        return sucessJSON("操作成功");
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(Integer id) {
        taskService.del(id);
        return sucessJSON("操作成功");
    }

    @RequestMapping("/info")
    public String info(Integer id, Integer type) {
        AppTask task = taskService.getById(id);
        request.setAttribute("obj", task);

        if(type == 1) {
            return this.sucess("/web/manage/app/task/detail");
        } else {
            return this.sucess("/web/manage/app/task/edit");
        }
    }

    @RequestMapping("/gotoAdd")
    public String gotoAdd() {
        return this.sucess("/web/manage/app/task/add");
    }
}
